﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace dsjsports
{
    class SynchronizationAddSportsmen : SynchronizationInfo
    {
        private SynchronizationIObject sender;
        public SynchronizationIObject Sender
        {
            get { return sender; }
        }

        private string tableName;
        public string TableName
        {
            get { return tableName; }
        }

        public SynchronizationAddSportsmen(SynchronizationIObject Sender, string TableName, string AdresDataaBase, int ID, string name, string middleName, string lastName, SportsRank sportsRank, DateTime dateOfBirth, Floor floor, string sity, string clab)
        {
            this.sender = Sender;
            this.tableName = TableName;
            this.adres = AdresDataaBase;
            this.id = ID;
            this.name = name;
            this.lastName = lastName;
            this.middleName = middleName;
            this.sportsRank = sportsRank;
            this.dateOfBirth = dateOfBirth;
            this.floor = floor;
            this.sity = sity;
            this.clab = clab;
        }

        private string adres;
        public string AdresDataaBase
        {
            get { return adres; }
        }

        private int id;
        public int ID
        {
            get { return id; }
        }

        private string name;
        public string Name
        {
            get { return name; }
        }

        private string lastName;
        public string LastName
        {
            get { return lastName; }
        }

        private string middleName;
        public string MiddleName
        {
            get { return middleName; }
        }

        private SportsRank sportsRank;
        public SportsRank SportsRank
        {
            get { return sportsRank; }
        }

        private DateTime dateOfBirth;
        public DateTime DateOfBirth
        {
            get { return dateOfBirth; }
        }

        private Floor floor;
        public Floor Floor
        {
            get { return floor; }
        }

        private string sity;
        public string Sity
        {
            get { return sity; }
        }

        private string clab;
        public string Clab
        {
            get { return clab; }
        }

        public bool Synchronization()
        {
            try
            {
                string setString = "SELECT спортсмен FROM спортсмен";

                OleDbConnection DataConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + adres);
                DataConnection.Open();

                OleDbCommand SQLQuery = new OleDbCommand(setString, DataConnection);
                OleDbDataAdapter dataAdapter = new OleDbDataAdapter(SQLQuery);
                DataTable data = new DataTable();
                dataAdapter.Fill(data);

                string updateString = "INSERT INTO спортсмен (спортсмен, имя, фамилия, отчество, пол, дата_рождения, разряд, город, клуб) " +
                    "VALUES (" + id + ", '" + name + "', '" + lastName + "', '" + middleName + "', '" + floor.ToString() + "', '" + dateOfBirth + "', '" + sportsRank.ToString() + "', '" + sity + "', '" + clab + "')";
                SQLQuery.CommandText = updateString;

                int intRecordsAffected = SQLQuery.ExecuteNonQuery();

                DataConnection.Close();

                sender.IsActual = true;
                return true;
            }
            catch
            {
                sender.IsActual = false;
                return false;
            }
        }
    }
}
